﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using QuickApp;
using QA.Wpf;
using System.ComponentModel;
using SQLSilver;
using QuickApp.DbInterface;

namespace SQLSilver.ViewModels
{
    public class DbBrowserViewModel : INotifyPropertyChanged
    {

        #region Private Fields
        private DbConnectionManagerViewModel dbConnectionManager = new DbConnectionManagerViewModel();
        private DataTable selectableTable;
        private object selectedObject;
        private IDbBrowserView view;

        #endregion Private Fields

        #region Public Constructors

        public DbBrowserViewModel(IDbBrowserView view)
        {
            this.view = view;
            dbConnectionManager.PropertyChanged += dbConnectionManager_PropertyChanged;
        }

        private void dbConnectionManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CurrentDbManager")
                onPropertyChanged("DbMetaData");
        }

        #endregion Public Constructors

        #region Public Events

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion Public Events

        #region Public Properties

        public DbConnectionManagerViewModel DbConnectionManager
        {
            get { return dbConnectionManager; }
        }

        public IQAParent DbMetaData
        {
            get
            {
                return dbConnectionManager.CurrentDbManager ;
            }
        }

        public int RowsToSelect
        {
            get;
            set;
        }

        public object SelectedObject
        {
            set
            {
                if (value != null)
                {
                    checkSetTable(value);
                    this.selectedObject = value;
                }
            }
        }

        public DataTable SelectedTable
        {
            private set
            {
                if(selectableTable != value)
                {
                    selectableTable = value;
                    onPropertyChanged("SelectedTable");
                }
            }
            get
            {
                QADbTable table = selectedObject as QADbTable;
                if (table != null)
                    return table.LastSelectedTable;
                return null;
            }
        }

        #endregion Public Properties

        #region Private Methods

        private void checkSetTable(object obj)
        {
            QADbTable table = obj as QADbTable;
            if (table != null)
            {
                QADbTable current = selectedObject as QADbTable;
                if (current != null && current.LastSelectedTable != null &&
                    current.LastSelectedTable.GetChanges() != null && view.PromptForSaveChanges())
                {
                    current.SaveLastSelect();
                }
                this.selectedObject = obj;
                SelectedTable = table.SelectTable(RowsToSelect);
            }
        }

        private void onPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private bool saveTableChanges()
        {
            QADbTable table = selectedObject as QADbTable;
            if (table != null && table.LastSelectedTable != null)
                return table.SaveLastSelect();
            return false;
        }

        #endregion Private Methods

    }
}
